#include "CProfileManager.h"

CProfileManager::~CProfileManager()
{
	///////////////////////////////////////////////////////////////////////////////
	//	Go through the linked list of clients that are able to receive this event.
	for(multimap<string, CProfiler*>::iterator mmIter = m_ProfileData.begin();
		mmIter != m_ProfileData.end(); mmIter++)
	{
		delete (*mmIter).second;
	}
	m_ProfileData.clear();
	//
	///////////////////////////////////////////////////////////////////////////////
}

CProfileManager* CProfileManager::GetInstance(void)
{
	static CProfileManager instance;
	return &instance;
}

void CProfileManager::StartProfile(string szName)
{
	///////////////////////////////////////////////////////////////////////////////
	//	Start a new Profile
	if(!AlreadyExists(szName))
	{
		m_pProfile = new CProfiler(szName);
		m_ProfileData.insert(make_pair(szName, m_pProfile));
	}
	//	Make an iterator that will iterate all of our clients that
	//	should be receiving this event
	pair<multimap<string, CProfiler*>::iterator,
		multimap<string, CProfiler*>::iterator> range;
	//	Find all of the clients that are able to receive this event.
	range = m_ProfileData.equal_range(szName);
	//	Go through the linked list of clients that are able to receive this event.
	for(multimap<string, CProfiler*>::iterator mmIter = range.first;
		mmIter != range.second; mmIter++)
	{
		//	Pass this event to the client
		(*mmIter).second->Start();
	}
	//
	///////////////////////////////////////////////////////////////////////////////
}
void CProfileManager::StopProfile(string szName)
{
	///////////////////////////////////////////////////////////////////////////////
	//	Make an iterator that will iterate all of our clients that
	//	should be receiving this event
	pair<multimap<string, CProfiler*>::iterator,
		multimap<string, CProfiler*>::iterator> range;
	//	Find all of the clients that are able to receive this event.
	range = m_ProfileData.equal_range(szName);
	//	Go through the linked list of clients that are able to receive this event.
	for(multimap<string, CProfiler*>::iterator mmIter = range.first;
		mmIter != range.second; mmIter++)
	{
		//	Pass this event to the client
		(*mmIter).second->Stop();
	}
	//
	///////////////////////////////////////////////////////////////////////////////
}

bool CProfileManager::AlreadyExists(string szName)
{
	bool bIsAlreadyRegistered = false;
	///////////////////////////////////////////////////////////////////////////////
	//	Make an iterator that will iterate all of our clients that
	//	should be receiving this event
	pair<multimap<string, CProfiler*>::iterator,
		multimap<string, CProfiler*>::iterator> range;
	//	Find all of the clients that are able to receive this event.
	range = m_ProfileData.equal_range(szName);
	//	Go through the list of clients that are able to receive this event.
	for(multimap<string, CProfiler*>::iterator mmIter = range.first;
		mmIter != range.second; mmIter++)
	{
		//	check if the pointer is equal to the client
		if((*mmIter).first == szName)
		{
			bIsAlreadyRegistered = true;
			break;
		}
	}
	//
	///////////////////////////////////////////////////////////////////////////////

	return bIsAlreadyRegistered;
}
